package cn.itcast.solr;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

import cn.itcast.pojo.Item;

public class SolrTest {

	public void aaa(){
		System.out.println("tianjiayige fangfa");
	}

	@Test
	public void createSolr() throws SolrServerException, IOException{
		//连接solr服务器
		HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		for(int i =0; i < 10; i++){
			//创建文档对象
			SolrInputDocument document = new SolrInputDocument();
			document.addField("id", i);
			document.addField("title", i+"华为手机,让你出彩");
			document.addField("price", 120000+10000*i);
			
			httpSolrServer.add(document);
			//提交
		}
	
		httpSolrServer.commit();
	}
	

	//通过pojo对象来创建索引
	@Test
	public void createSolr2() throws IOException, SolrServerException{
		//获取solr连接服务
		HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
		//创建solr文档
		SolrInputDocument  solrInputDocument = new SolrInputDocument();
		//创建Item对象
		Item item = new Item();
		item.setId("10");
		item.setTitle("tianyahaijiao诺基亚手机,王朝的落寞");
		item.setPrice(10000);
		//提交
		httpSolrServer.addBean(item);
		
		httpSolrServer.commit();
	}
	
	//删除文档
	@Test
	public void deleteDoc() throws Exception{
		//连接服务
		HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		//删除文档
		httpSolrServer.deleteById("change.me");
		//提交
		httpSolrServer.commit();
	}
	
	@Test
	public void deleteDoc2() throws SolrServerException, IOException{
		//获取连接服务
		HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
		//删除文档
		httpSolrServer.deleteByQuery("id:1");
		//提交
		httpSolrServer.commit();
		
	}
	
	//查询document
	@Test
	public void queryDoc() throws SolrServerException{
		//获取连接服务
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		//查询
		SolrParams params = new SolrQuery("*:*");
		QueryResponse response = server.query(params);
		
		SolrDocumentList results = response.getResults();
		System.out.println("一共"+results.size());
		for (SolrDocument solrDocument : results) {
			System.out.println(solrDocument.getFieldValue("title"));
			
		}
	}
	
	@Test
	public void queryDoc2() throws SolrServerException{
		//获取连接服务
		HttpSolrServer server  = new HttpSolrServer("http://localhost:8080/solr/core1");
		//查询参数
		SolrParams params = new SolrQuery("*:*");
		//查询
		QueryResponse response = server.query(params);
		//获取结果
		List<Item> beans = response.getBeans(Item.class);
		for (Item item : beans) {
			System.out.println(item);
		}
	}
	
	@Test
	public void queryDoc3() throws SolrServerException{
		//获取连接服务
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		//查询参数
		SolrQuery query = new SolrQuery("title:手机");
		//查询
		QueryResponse response = server.query(query);
		
		List<Item> beans = response.getBeans(Item.class);
		for (Item item : beans) {
			System.out.println(item);
			
		}
		
	}
	
	//solrquery的特殊查询
	@Test
	public void queryDoc4() throws SolrServerException{
		//获取连接
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		//SolrQuery query = new SolrQuery("id:2 OR title:王朝");
		
		SolrQuery query = new SolrQuery("id:2 AND title:手机  OR price:20");
		
		QueryResponse response = server.query(query);
		
		List<Item> beans = response.getBeans(Item.class);
		
		for (Item item : beans) {
			System.out.println(item);
			
		}
	}
	
	//子表达式查询(query1 AND query2) OR (query3 AND query4)
	@Test
	public void queryTest(){
		
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		SolrQuery query1 = new SolrQuery("id:*");
		SolrQuery query2 = new SolrQuery("title:手机");
		SolrQuery query3 = new SolrQuery("price:150000");
		SolrQuery query4 = new SolrQuery("price:160000 OR price:180000");
	}
	
	
	//相似度查询fuzzy
	@Test
	public void queryTest1() throws SolrServerException{
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		SolrQuery query = new SolrQuery("title:tianyahaajiaa~");
		
		QueryResponse response = server.query(query);
		
		List<Item> beans = response.getBeans(Item.class);
		
		for (Item item : beans) {
			System.out.println(item);
		}
	}
	
	//范围查询
	@Test
	public void rangeQuery() throws SolrServerException{
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		SolrQuery query = new SolrQuery("title:手机 AND id:[1 TO 4]");
		
		QueryResponse response = server.query(query);
		
		List<Item> beans = response.getBeans(Item.class);
		
		for (Item item : beans) {
			System.out.println(item);
		}
	}
	
	//排序
	@Test
	public void sortQuery() throws SolrServerException{
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		SolrQuery query = new SolrQuery("id:[1 TO 9]");
		
		query.setSort("id", ORDER.asc);
		
		QueryResponse response = server.query(query);
		
		List<Item> beans = response.getBeans(Item.class);
		
		for (Item item : beans) {
			System.out.println(item);
		}
	}
	
	//分页
	@Test
	public void pageQuery() throws SolrServerException{
		int pageNum = 2;
		int pageSize = 5;
		
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
		
		SolrQuery query = new SolrQuery("title:手机");
		query.setSort("id", ORDER.desc);
		
		query.setStart((pageNum-1)*pageSize);
		query.setRows(pageSize);
		
		query.setHighlight(true);
		query.setHighlightSimplePre("<em>");
		query.setHighlightSimplePost("</em>");
		
		query.addHighlightField("title");
		
		QueryResponse response = server.query(query);
		
		SolrDocumentList results = response.getResults();
		
		//外的key为 id,内的key为查询的字段title
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		
		
		
		for (SolrDocument solrDocument : results) {
			String id =(String) solrDocument.getFieldValue("id");
			System.out.println("id:"+id);
			Map<String, List<String>> map = highlighting.get(id);
			System.out.println("title:"+map.get("title").get(0));
			
		}
		
	}
	
}



